package com.example.blog_system_ssm.service;

import cn.hutool.core.util.IdUtil;
import com.example.blog_system_ssm.component.AjaxResult;
import com.example.blog_system_ssm.component.PasswordTools;
import com.example.blog_system_ssm.entity.UserEntity;
import com.example.blog_system_ssm.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @Resource
    private MailService mailService;


    @Transactional
    public AjaxResult createAccount(UserEntity userEntity){
        //  1) 将密码进行加盐加密操作,生成加盐加密后的密码
        String password = PasswordTools.encrypt(userEntity.getPassword());

        // 2) 利用雪花算法生成用户确认码
        String confirmCode = IdUtil.getSnowflake(1,1).nextIdStr();

        // 3) 激活失效时间:10分钟
        LocalDateTime activationTime = LocalDateTime.now().plusMinutes(10);

        // 测试时间
        // LocalDateTime activationTime = LocalDateTime.now().plusMinutes(1);

        // 4) 将这些属性值添加至UserEntity中
        userEntity.setPassword(password);
        userEntity.setConfirmCode(confirmCode);
        userEntity.setActivationTime(activationTime);
        // 5) 新增用户，成功会返回 1
        int result = userMapper.insertUser(userEntity);
        if(result > 0){
            // 发送邮件
            String activationCode = confirmCode;
            mailService.sendMailForActivationAccount(activationCode,userEntity.getEmail());
            return AjaxResult.success(result);
        }
        return AjaxResult.fail(-2,"用户注册失败");
    }

    public UserEntity loginAccount(String username){
        return userMapper.selectByName(username);
    }

    public UserEntity selectByName(String username){
        return userMapper.selectByName(username);
    }


    // 激活账号
    //@Transactional
    public AjaxResult activationAccount(String confirmCode,String username) {
        // 根据用户名查询用户
        UserEntity userEntity = userMapper.selectByName(username);
        // 查询结果为空,用户不存在
        if(userEntity == null){
            return AjaxResult.fail(-2,"用户名有误！");
        }
        // 用户账号已激活，无需重复激活
        if(userEntity.getState() == 1){
            return AjaxResult.fail(-4,"用户账号已激活");
        }
        // 比较所填的激活码与数据表中的是否一致
        if(!userEntity.getConfirmCode().equals(confirmCode)){
            return AjaxResult.fail(-2,"激活码有误！");
        }
        // 判断激活时间是否超时
        Boolean after = LocalDateTime.now().isAfter(userEntity.getActivationTime());
        if(after == true){
            // 未在激活时间内激活，请重新注册之前应先把失效账号删除
            deleteAccount(confirmCode,username);
            return AjaxResult.fail(-3,"未在激活时间内激活，请重新注册");
        }
        // 根据确认码查询并修改用户并修改状态值
        int result = userMapper.updateByConfirmCode(confirmCode,username);
        if(result > 0){
            return AjaxResult.success(result);
        }
        return AjaxResult.fail(-5,"用户激活失败！");
    }

    // 删除激活失效的用户账号
    public void deleteAccount(String confirmCode, String username){
        int result = userMapper.deleteAccount(confirmCode,username);
        if(result > 0){
            System.out.println("删除激活失效的账号成功！");
        }else{
            System.out.println("删除激活失效的账号失败！");
        }
    }


//    // 激活账号
//    @Transactional
//    public AjaxResult activationAccount(String confirmCode) {
//        // 根据确认码查询用户
//        UserEntity userEntity = userMapper.selectByConfirmCode(confirmCode);
//        // 判断激活时间是否超时
//        Boolean after = LocalDateTime.now().isAfter(userEntity.getActivationTime());
//        if(after == true){
//           // 链接也失效，请重新注册之前应先把失效账号删除
//            deleteAccount(confirmCode);
//            return AjaxResult.fail(-2,"用户激活链接也失效，请重新注册");
//        }
//        // 根据确认码查询并修改用户并修改状态值
//        int result = userMapper.updateByConfirmCode(confirmCode);
//        if(result > 0){
//            return AjaxResult.success(200,"用户激活成功！");
//        }
//        return AjaxResult.fail(-2,"用户激活失败！");
//    }


//    // 删除激活失效的用户账号
//    public void deleteAccount(String confirmCode){
//        int result = userMapper.deleteAccount(confirmCode);
//        if(result > 0){
//            System.out.println("删除激活失效的账号成功！");
//        }else{
//            System.out.println("删除激活失效的账号失败！");
//        }
//    }

}
